<template>
  <!--搜索-->
  <!--    搜索框-->
  <div style="padding: 10px 0">
    <el-input style="width: 200px" placeholder="请输入身份证号" clearable v-model="searchuserid"></el-input>
    <el-input style="width: 150px; margin-left: 15px;" placeholder="请输入疫苗编号" clearable
      v-model="searchvaccinid"></el-input>

    <el-button type="primary" style="margin-left: 15px" @click="getReservationAll">
      <el-icon style="vertical-align: middle">
        <search />
      </el-icon>
      <span style="vertical-align: middle"> 搜索 </span>
    </el-button>
    <!-- 添加按钮 -->
    <el-button type="primary" style="margin-left: 15px" @click="adduser">
      <el-icon style="margin-right: 5px">
        <CirclePlusFilled />
      </el-icon>
      添加
    </el-button>
  </div>
  <!--列表-->
  <el-table :data="tableData" style="width: 100%" height="550">
    <el-table-column fixed prop="id" label="序号" width="80" align="center" />
    <el-table-column prop="userid" label="身份证号" width="180" align="center" />
    <el-table-column prop="user.name" label="姓名" width="60" align="center">
      <template #reference>
        <el-tag>{{ scope.row.user.name }}</el-tag>
      </template>
    </el-table-column>
    <!-- <el-table-column prop="user.telephone" label="电话号码" width="180" align="center"/>
    <el-table-column prop="user.whether" label="接种针数" width="80" align="center"/> -->
    <el-table-column prop="vaccinid" label="疫苗编号" width="160" align="center" />
    <el-table-column prop="vaccin.vname" label="疫苗名称" width="200" align="center">
      <template #default="scope">
        <el-popover effect="light" trigger="hover" placement="top" width="auto">
          <template #default>
            <div>疫苗名称: {{ scope.row.vaccin.vname }}</div>
            <div>疫苗等级: {{ scope.row.vaccin.vrank }}</div>
            <div>疫苗类型: {{ scope.row.vaccin.vsort }}</div>
            <div>生产公司: {{ scope.row.vaccin.maker }}</div>
          </template>
          <template #reference>
            <el-tag>{{ scope.row.vaccin.vname }}</el-tag>
          </template>
        </el-popover>
      </template>
    </el-table-column>
    <!-- <el-table-column prop="vaccin.vrank" label="疫苗等级" width="80" align="center"/>
    <el-table-column prop="vaccin.vsort" label="疫苗类型" width="80" align="center"/>
    <el-table-column prop="vaccin.maker" label="疫苗生产企业" width="250" align="center"/> -->
    <el-table-column prop="jabnum" label="接种针数" width="80" align="center" />
    <el-table-column prop="userbody" label="身体状况" width="80" align="center" />
    <el-table-column prop="isallergy" label="是否有过敏史" width="120" align="center">
      <!-- 根据表中的值0和1生成一个按钮 -->
      <template v-slot="scope">
        <el-switch v-model="scope.row.isallergy" inline-prompt active-color="#6495ED" inactive-color="#B0C4DE"
          active-text="是" inactive-text="否" :active-value="1" :inactive-value="0" disabled />
      </template>
    </el-table-column>
    <el-table-column prop="adress" label="预约地址" width="160" align="center" />
    <el-table-column prop="reservationtime" label="预约时间" width="180" align="center" />
    <el-table-column prop="state" fixed="right" label="完成状态" width="80" align="center">
      <!-- 根据表中的值0和1生成一个按钮 -->
      <template v-slot="scope">
        <el-switch v-model="scope.row.state" inline-prompt active-color="#409EFF" inactive-color="#E6A23C"
          active-text="是" inactive-text="否" :active-value="0" :inactive-value="1" @change="isshow(scope.row)" />
      </template>
    </el-table-column>
    <el-table-column fixed="right" label="操作" width="140" align="center">
      <template #default="scope">
        <el-button type="warning" size="small" @click="useredit(scope.row)">编辑</el-button>
        <el-button type="danger" size="small" @click="deluser(scope.row.id)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
  <!--分页-->
  <div style="padding: 10px 0">
    <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
      v-model:currentPage="pageNum" :page-sizes="[10, 20, 30, 50]" v-model:page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
  </div>
  <!-- 编辑或者添加的弹窗 -->
  <el-dialog title="预约疫苗接种" v-model="dialogFormVisible" width="50%">
    <el-form :model="form" size="default" :rules="rules" ref="form">
      <el-row>
        <el-col :span="18">
          <el-form-item label="身份证号" label-width="100px" prop="userid">
            <el-input v-model="form.userid" autocomplete="off"></el-input> </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="10">
          <el-form-item label="疫苗编号" label-width="100px" prop="vaccinid">
            <el-input v-model="form.vaccinid" autocomplete="off"></el-input> </el-form-item></el-col>
        <el-col :span="8">
          <el-form-item label="接种针数" label-width="100px" prop="jabnum">
            <el-input v-model="form.jabnum" autocomplete="off"></el-input> </el-form-item></el-col>
      </el-row>
      <el-row>
        <el-col :span="18">
          <el-form-item label="健康水平" label-width="100px" prop="userbody">
            <el-input v-model="form.userbody" autocomplete="off"></el-input>
          </el-form-item> </el-col></el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="是否有过敏史" label-width="100px" prop="isallergy">
            <el-select v-model="form.isallergy">
              <el-option label="1 (是)" value="1" />
              <el-option label="0 (否)" value="0" />
            </el-select> </el-form-item></el-col></el-row>
      <el-row>
        <el-col :span="18">
          <el-form-item label="接种地址" label-width="100px" prop="adress">
            <el-input v-model="form.adress" autocomplete="off"></el-input> </el-form-item></el-col></el-row>
      <el-row>
        <el-col :span="8">
          <el-form-item label="接种时间" label-width="100px" prop="reservationtime">
            <el-date-picker v-model="form.reservationtime" type="datetime" placeholder="选择日期和时间"
              :size="size" /></el-form-item></el-col></el-row>
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogFormVisible = false"> 取消</el-button>
        <el-button type="primary" @click="saveData('form')"> 确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { ElMessage, ElMessageBox } from "element-plus";
export default {
  name: "Reservation",
  data() {
    return {
      tableData: [],
      total: 0, //数据总条数
      pageNum: 1, //当前页
      pageSize: 10, //页大小
      searchuserid: "", //搜索证号
      searchvaccinid: "", //搜索疫苗编号
      dialogFormVisible: false,
      form: {},
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
        ],
        avatar: [
          { required: true, message: "请输入头像地址", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    isshow(row) { //状态改变
      ElMessageBox.confirm(
        '确定要改变状态吗？',
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        },
        console.log(row),
      ).then(() => {
        // row.isShow = !row.isShow //点击后，默认已经改变了，所以无需再手动改变
        let token = localStorage.getItem("token");
        this.$http
          .post("inoculations/reservation/save", row, token).then((res) => {
            if (res.statusCode == '200') {
              ElMessage({
                message: '状态修改成功！',
                type: 'success',
              })
              this.getblogAll()
            } else {
              this.getblogAll()
              ElMessage.error('失败！')
            }
          }).catch(() => {
            // row.isShow = !row.isShow
            this.getblogAll()
            ElMessage.error('失败！')
          })
      }).catch(() => {
        this.getblogAll()
        ElMessage({
          type: 'info',
          message: '已取消！',
        })
      })

    },
    handleSizeChange(val) {
      //页大小改变
      this.pageSize = val;
      this.getReservationAll();
    },
    handleCurrentChange(val) {
      //当前页改变
      this.pageNum = val;
      this.getReservationAll();
    },
    //分页查询
    getReservationAll() {
      this.$http
        .get(
          "/inoculations/reservation/pageall",
          {
            params: {
              pageNum: this.pageNum,
              pageSize: this.pageSize,
              userid: this.searchuserid,
              vaccinid: this.searchvaccinid,
            },
          }
        )
        .then((res) => {
          if (res.statusCode == "200") {
            this.total = res.data.total;
            this.tableData = res.data.records;
            console.log(res.data.records);
          }
        })
        .catch(() => {
          ElMessage.error("数据加载失败,请刷新！");
        });
    },
    // 编辑
    useredit(row) {
      this.form = row;
      this.dialogFormVisible = true;
    },
    // 添加
    adduser() {
      this.form = {};
      this.dialogFormVisible = true;
    },
    //用于数据的添加和更新
    saveData() {
      this.$http
        .post("/inoculations/reservation/save", this.form)
        .then((res) => {
          if (res.statusCode == "200") {
            ElMessage({
              message: "成功！",
              type: "success",
            });
            this.dialogFormVisible = false;
            this.getReservationAll();
          }
        })
        .catch(() => {
          ElMessage.error("失败！");
        });
    },

    // 删除
    deluser(id) {
      ElMessageBox.confirm("是否删除选中数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          console.log(id);
          let token = localStorage.getItem("token");
          this.$http
            .delete("/inoculations/reservation/del/" + id, token)
            .then((res) => {
              if (res.statusCode == "200") {
                ElMessage({
                  message: "删除成功！",
                  type: "success",
                });
                this.getuserDate();
              }
            })
            .catch(() => {
              ElMessage.error("删除失败！");
            });
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "已取消！",
          });
        });
    },
  },
  created() {
    this.getReservationAll();
  },
};
</script>

<style scoped>
.el-button+span {
  margin-left: 10px;
}
</style>
